package com.ucap.weking.guangan.fragments.fragmentContent;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.google.gson.Gson;
import com.ucap.weking.guangan.R;
import com.ucap.weking.guangan.base.BaseFragment;
import com.ucap.weking.guangan.bean.LeaderItemBean;
import com.ucap.weking.guangan.dataUri.DataUri;
import com.ucap.weking.guangan.httpTools.HttpCallRequest;
import com.ucap.weking.guangan.modle.LeaderNameModel;
import com.ucap.weking.guangan.tools.PrefUtils;
import com.ucap.weking.guangan.views.LeaderItem;
import com.ucap.weking.guangan.views.MyToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.InjectViews;

/**
 * Created by weking on 2017/10/23.
 */

public class HomeContent2Laeder extends BaseFragment {

    @InjectViews({R.id.li_leader_1, R.id.li_leader_2, R.id.li_leader_3, R.id.li_leader_4})
    LeaderItem[] leaderItem1;
    @InjectView(R.id.ll_root)
    LinearLayout llRoot;
    @InjectView(R.id.pb_loading)
    ProgressBar pbContent;
    private String leader_data;

    @Override
    public void setData() {
        leader_data = PrefUtils.getString("leader_data", "", getContext());
        if (!leader_data.equals(""))
            analysis(leader_data);
        getData();
    }

    @Override
    public void intiView() {
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.home_conetnt3_leader, null);
        flContent.addView(inflate);
        ButterKnife.inject(this, inflate);

    }

    public void getData() {
        llRoot.setVisibility(View.INVISIBLE);
        pbContent.setVisibility(View.VISIBLE);
        HttpCallRequest.callRequest(DataUri.LADER_URI, new HttpCallRequest.OnCallListener() {
            @Override
            public void onRequest(final String body) {
                PrefUtils.putString("leader_data", body, getContext());
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (leader_data.equals("")) {
                            analysis(body);
                        }
                        llRoot.setVisibility(View.VISIBLE);
                        pbContent.setVisibility(View.GONE);
                    }
                });
            }

            @Override
            public void onErr(Exception e) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//                        MyToast.showTrue(getContext(), "连接失败！");
                        llRoot.setVisibility(View.VISIBLE);
                        pbContent.setVisibility(View.GONE);
                    }
                });

            }
        });
    }

    public void analysis(String data) {
        try {
            Log.i("LeaderData", "onRequest: " + data);
            Gson gson = new Gson();
            LeaderItemBean leaderItemBean = gson.fromJson(data, LeaderItemBean.class);
            List<LeaderItemBean.Rows1Bean> rows1 = leaderItemBean.getRows1();
            List<LeaderItemBean.Rows2Bean> rows2 = leaderItemBean.getRows2();
            List<LeaderItemBean.Rows3Bean> rows3 = leaderItemBean.getRows3();
            List<LeaderItemBean.Rows4Bean> rows4 = leaderItemBean.getRows4();
            ArrayList<LeaderNameModel> shiWeiShuJi = new ArrayList<>();
            ArrayList<LeaderNameModel> shiZhang = new ArrayList<>();
            ArrayList<LeaderNameModel> fuShiZhang = new ArrayList<>();
            ArrayList<LeaderNameModel> miShuZhang = new ArrayList<>();
            if (rows1 != null && rows1.size() > 0) {
                for (LeaderItemBean.Rows1Bean bean1 : rows1) {
                    LeaderNameModel model1 = new LeaderNameModel(bean1.getTITLE(), bean1.getPATHANDURL(), bean1.getURL());
                    model1.setTypeName(bean1.getCHANNELNAME());
                    shiWeiShuJi.add(model1);
                }
                leaderItem1[0].setData(shiWeiShuJi);
            }
            if (rows2 != null && rows2.size() > 0) {
                for (LeaderItemBean.Rows2Bean bean2 : rows2) {
                    LeaderNameModel model2 = new LeaderNameModel(bean2.getTITLE(), bean2.getPATHANDURL(), bean2.getURL());
                    model2.setTypeName(bean2.getCHANNELNAME());
                    shiZhang.add(model2);
                }
                leaderItem1[1].setData(shiZhang);
            }
            if (rows3 != null && rows3.size() > 0) {
                for (LeaderItemBean.Rows3Bean bean3 : rows3) {
                    LeaderNameModel model3 = new LeaderNameModel(bean3.getTITLE(), bean3.getPATHANDURL(), bean3.getURL());
                    model3.setTypeName(bean3.getCHANNELNAME());
                    fuShiZhang.add(model3);
                }
                leaderItem1[2].setData(fuShiZhang);
            }
            if (rows4 != null && rows4.size() > 0) {
                for (LeaderItemBean.Rows4Bean bean4 : rows4) {
                    LeaderNameModel model4 = new LeaderNameModel(bean4.getTITLE(), bean4.getPATHANDURL(), bean4.getURL());
                    model4.setTypeName(bean4.getCHANNELNAME());
                    miShuZhang.add(model4);
                }
                leaderItem1[3].setData(miShuZhang);
            }

        } catch (Exception e) {

        }
    }


}
